/* Procedure to get inventory qty by loc/date 3.6.16 logic */
/* need to add begin five cost */
/* TEMP-TABLE STORING QUANTITY, ITEM NO. AND LOCATION          */
/* FOR A SITE
                                                 */
/*												 
using com.qad.eefin.buser.EntitySecurityCheck.
*/
/*UAT-CR*/
&scoped-define TimeStamp STRING(MONTH(TODAY),"99") + STRING(DAY(TODAY),"99") +~
                         STRING(YEAR(TODAY),"9999") +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),1,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),4,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),7,2)
                         
{us/mf/mfdtitle.i}
{us/gp/gpglefdf.i }

/* DEFINING VARIABLES AS NO-UNDO */
{us/px/pxmaint.i}
{us/px/pxphdef.i ppitxr}
{us/px/pxphdef.i ppptexpl}
												

/* Variable definitions */
DEFINE VARIABLE entity              LIKE  en_entity   FORMAT "x(8)"     NO-UNDO.
DEFINE VARIABLE site                LIKE  si_site                       NO-UNDO.
DEFINE VARIABLE li_year             LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_period           LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE li_cur_yr           LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_cur_per          LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE ld_start_dt         LIKE  glc_start                     NO-UNDO.
DEFINE VARIABLE ld_end_dt           LIKE  glc_end                       NO-UNDO.
DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
DEFINE VARIABLE lc_tmpstr           AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE li_success          AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE lc_tmpyn            AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE li_last_trnbr       LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_optrnbr     LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_aptrnbr     LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_poso        LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_wo          LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_inv         LIKE  tr_hist.tr_trnbr                      NO-UNDO.
DEFINE VARIABLE lc_log_file         AS    CHARACTER   FORMAT "x(40)"    NO-UNDO. 
DEFINE VARIABLE lc_to_code          AS    CHARACTER   INIT "UTF-8"      NO-UNDO. 


/* For calling QAD Financials Data Object to check GL Entity Security */
/*
DEFINE VARIABLE hQADFinancialsLib   AS    HANDLE                        NO-UNDO.
DEFINE VARIABLE EntitySecurity      AS    EntitySecurityCheck           NO-UNDO.
DEFINE VARIABLE valid_entity        LIKE  mfc_logical                   NO-UNDO.
DEFINE VARIABLE BufErrorMessage     AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE ErrorMessage        AS    CHARACTER                     NO-UNDO.
	*/											  
define variable std_as_of     like glxcst       no-undo.	
define variable l_glx_mthd as character no-undo.
define variable l_glx_set  as character no-undo.
define variable l_cur_mthd as character no-undo.
define variable l_cur_set  as character no-undo.	
define variable tr_recno      as   recid        no-undo.
define variable trrecno       as   recid        no-undo.		
define variable cst_date      like tr_hist.tr_effdate   no-undo.	
define variable debug-prog    as   logical      no-undo.
			  
												  
DEFINE TEMP-TABLE t_trhist NO-UNDO
   FIELD t_trhist_domain             LIKE tr_hist.tr_domain
   FIELD t_trhist_part               LIKE tr_hist.tr_part
   FIELD t_trhist_effdate            LIKE tr_hist.tr_effdate
   FIELD t_trhist_site               LIKE tr_hist.tr_site
   FIELD t_trhist_loc                LIKE tr_hist.tr_loc
   FIELD t_trhist_trnbr              LIKE tr_hist.tr_trnbr
   FIELD t_trhist_ship_type          LIKE tr_hist.tr_ship_type
   FIELD t_trhist_nbr                LIKE tr_hist.tr_nbr
   FIELD t_trhist_type               LIKE tr_hist.tr_type
   FIELD t_trhist_program            LIKE tr_hist.tr_program
   FIELD t_trhist_qty_loc            LIKE tr_hist.tr_qty_loc
   FIELD t_trhist_status             LIKE tr_hist.tr_status
   FIELD t_trhist_rmks               LIKE tr_hist.tr_rmks
   FIELD t_trhist_rev                LIKE tr_hist.tr_rev
   FIELD t_trhist_qty_cn_adj         LIKE tr_hist.tr_qty_loc
   INDEX t_trhist IS PRIMARY UNIQUE
      t_trhist_domain t_trhist_part t_trhist_effdate t_trhist_site
      t_trhist_loc t_trhist_trnbr t_trhist_ship_type.
 
/* TEMP-TABLE STORING QUANTITY ON HAND, ITEM NO. AND LOCATION */
/* FOR A SITE                                                 */
DEFINE TEMP-TABLE t_lddet NO-UNDO
   FIELD t_lddet_site               LIKE ld_site
   FIELD t_lddet_part               LIKE ld_part
   FIELD t_lddet_loc                LIKE ld_loc
   FIELD t_lddet_qty                LIKE in_qty_oh
   FIELD t_lddet_avail_stat         LIKE mfc_logical
   FIELD t_lddet_net                LIKE mfc_logical
   FIELD t_lddet_cust_consign_qty   LIKE ld_cust_consign_qty
   FIELD t_lddet_supp_consign_qty   LIKE ld_supp_consign_qty
   INDEX t_lddet IS PRIMARY UNIQUE 
   t_lddet_site t_lddet_part t_lddet_loc.
 
define temp-table t_lddet2 NO-UNDO
   FIELD t_lddet2_site  LIKE ld_site
   FIELD t_lddet2_part  LIKE ld_part
   FIELD t_lddet2_loc   LIKE ld_loc
   FIELD t_lddet2_qty   LIKE in_qty_oh
   field t_lddet2_tot       like tr_hist.tr_mtl_std   
   field t_lddet2_mtl       like tr_hist.tr_mtl_std
   field t_lddet2_lbr       like tr_hist.tr_lbr_std
   field t_lddet2_bdn       like tr_hist.tr_bdn_std
   field t_lddet2_ovh      like tr_hist.tr_ovh_std
   field t_lddet2_sub       like tr_hist.tr_sub_std 
   INDEX t_lddet2 IS PRIMARY UNIQUE 
   t_lddet2_site t_lddet2_part t_lddet2_loc.
 
/* TEMP-TABLE STORING INVENTORY STATUS TO AVOID MULTIPLE SCANNING */
/* OF IS_MSTR                                                     */
DEFINE TEMP-TABLE t_stat NO-UNDO
   FIELD t_stat_stat LIKE is_status
   FIELD t_stat_net  LIKE is_nettable
   INDEX t_stat IS PRIMARY UNIQUE
   t_stat_stat.

/* TEMP-TABLE STORING GL COST, ITEM DESCRIPTION, UM AND ITEM ABC. */
/* FOR ALL SITES                                                  */
define temp-table t_sct no-undo
   field t_sct_part  like pt_part
   field t_sct_desc1     like pt_desc1
   field t_sct_desc2     like pt_desc2
   field t_sct_um        like pt_um
   field t_sct_abc       like pt_abc
   field t_sct_mtl       like tr_hist.tr_mtl_std
   field t_sct_lbr       like tr_hist.tr_lbr_std
   field t_sct_bdn       like tr_hist.tr_bdn_std
   field t_sct_ovh       like tr_hist.tr_ovh_std
   field t_sct_sub       like tr_hist.tr_sub_std 
   field t_sct_tot       like sct_cst_tot   
   field t_sct_std_as_of like tr_hist.tr_effdate
   index t_sct is primary unique
   t_sct_part.

   define variable ptpart like pt_part no-undo.

	
FORM
    entity     COLON 20 
    site       COLON 20 
    li_year    COLON 20 
    li_period  COLON 20 

    WITH FRAME a WIDTH 80 SIDE-LABELS .
    setFrameLabels(FRAME a:HANDLE).

/* Main Loop starts */
mainloop:
REPEAT TRANSACTION ON ERROR UNDO mainloop, RETRY mainloop:
   
   ASSIGN entity           = ""
          site             = ""
          li_year          = YEAR(TODAY)
          li_period        = MONTH(TODAY)
          lc_tmpstr        = ""
          lc_tmpyn         = NO
          li_last_inv      = 0
          li_success       = YES
          /*UAT-CR*/
          lc_log_file      = "history_load_" +
                             {&TimeStamp} + ".txt"
          .
   
 
   EMPTY TEMP-TABLE t_lddet2.
   
   FIND FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                                AND glc_start <= TODAY
                                AND glc_end   >= TODAY NO-ERROR.
   IF AVAILABLE glc_cal THEN 
      ASSIGN li_cur_yr  = glc_year 
             li_cur_per = glc_per
             li_year    = glc_year
             li_period  = glc_per.
   
   CLEAR FRAME a NO-PAUSE.
   
   UPDATE entity   
          WITH FRAME a EDITING:
          
      {us/mf/mfnp05.i en_mstr en_entity "en_domain = global_domain" 
                      en_entity "input entity "}
      IF recno <> ? THEN DO:
         ASSIGN entity = en_entity.
         DISPLAY
            entity
         WITH FRAME a.
      END.         
   END. /* UPDATE EDITING entity */
   
   
   IF NOT CAN-FIND( FIRST en_mstr WHERE en_domain = global_domain 
                                   AND en_entity = entity)
   THEN DO:
      /* Invalid entity */
      {us/bbi/pxmsg.i &MSGNUM=3061 &ERRORLEVEL=3}
      NEXT-PROMPT entity WITH FRAME a.
      UNDO, RETRY.
   END. /* IF NOT CAN-FIND(FIRST */
      /*
   /* CHECK ENTITY SECURITY */
   IF entity <> "" THEN DO:    
      {us/gl/glenchk.i &entity=entity &entity_ok=valid_entity} 
      IF NOT valid_entity THEN DO:
         NEXT-PROMPT entity WITH FRAME a.
         UNDO, RETRY.
      END.     
   END. /* IF entity <> "" */
   */
   siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE site     
           WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input site"}
            IF recno <> ? THEN DO:
               ASSIGN site = si_site.
               DISPLAY site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = site
									  AND si_entity = entity) 
      THEN DO:
         /* Invalid Site */
         {us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* Site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */
   
   yrloop:
   DO ON ERROR UNDO yrloop, RETRY yrloop:
   
         UPDATE li_year  
             li_period WITH FRAME a.
       /*      
      IF LENGTH(li_year) <> 4 THEN DO:
         {us/bbi/pxmsg.i &msgnum=3900 &errorlevel=1 &msgarg1=""Year"" }
         NEXT-PROMPT li_year.
         UNDO yrloop, RETRY yrloop.
      END.
            
      IF li_period < 1 OR li_period > 12 THEN DO:
         {us/bbi/pxmsg.i &msgnum=282 &errorlevel=1 &msgarg1=""Period"" }
         NEXT-PROMPT li_period.
         UNDO yrloop, RETRY yrloop.
      END.
      */
   END. /* yrloop */ 
/*
message "run" view-as alert-box.
*/


   debug-prog = no.
   /* Get start & end date for the input year/period from Gl Calendar */
   FOR FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                               AND glc_year   = li_year
                               AND glc_per    = li_period:
   END.
   IF AVAILABLE glc_cal THEN 
      ASSIGN ld_start_dt = glc_start
             ld_end_dt   = glc_end
             .
   ELSE 
      ASSIGN ld_start_dt = DATE(li_period,1,li_year)
             ld_end_dt   = ADD-INTERVAL(ld_start_dt,1,"MONTH") - 1
             .
	for each xxcsttrinvd_det where xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
	                       and xxcsttrinvd_det.xxcsttrinvd_entity = entity
						   and xxcsttrinvd_det.xxcsttrinvd_site = site
						   and xxcsttrinvd_det.xxcsttrinvd_year = li_year
						   and xxcsttrinvd_det.xxcsttrinvd_period = li_period
						   .
		if debug-prog then 
		status default xxcsttrinvd_det.xxcsttrinvd_part + " Del".
		delete xxcsttrinvd_det.
		
	end.
	Run Get_Inv_Qty(input site, input ld_start_dt - 1, BUFFER t_lddet2).
  
	for each t_lddet2 .
	find xxcsttrinvd_det where xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
	                       and xxcsttrinvd_det.xxcsttrinvd_entity = entity
						   and xxcsttrinvd_det.xxcsttrinvd_site = site
						   and xxcsttrinvd_det.xxcsttrinvd_year = li_year
						   and xxcsttrinvd_det.xxcsttrinvd_period = li_period
						   and xxcsttrinvd_det.xxcsttrinvd_part = t_lddet2_part
						   and xxcsttrinvd_det.xxcsttrinvd_loc = t_lddet2_loc

						   no-error.
	if not available xxcsttrinvd_det then do:
      create xxcsttrinvd_det.
	  assign xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
	         xxcsttrinvd_det.xxcsttrinvd_entity = entity
	         xxcsttrinvd_det.xxcsttrinvd_site = site
	         xxcsttrinvd_det.xxcsttrinvd_year = li_year
	         xxcsttrinvd_det.xxcsttrinvd_period = li_period
			 xxcsttrinvd_det.xxcsttrinvd_part = t_lddet2.t_lddet2_part
	         xxcsttrinvd_det.xxcsttrinvd_loc = t_lddet2.t_lddet2_loc.
	end.
	 assign xxcsttrinvd_det.xxcsttrinvd_begin_inv  = t_lddet2.t_lddet2_qty
	        xxcsttrinvd_det.xxcsttrvar_mtl_std_beg = t_lddet2.t_lddet2_mtl
            xxcsttrinvd_det.xxcsttrvar_lbr_std_beg = t_lddet2.t_lddet2_lbr
            xxcsttrinvd_det.xxcsttrvar_bdn_std_beg = t_lddet2.t_lddet2_bdn
            xxcsttrinvd_det.xxcsttrvar_ovh_std_beg = t_lddet2.t_lddet2_ovh
            xxcsttrinvd_det.xxcsttrvar_sub_std_beg = t_lddet2.t_lddet2_sub.
		 	
	  
	end.
	Run Get_Inv_Qty(input site, input ld_end_dt , BUFFER t_lddet2).
	for each t_lddet2 .
    find xxcsttrinvd_det where xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
	                       and xxcsttrinvd_det.xxcsttrinvd_entity = entity
						   and xxcsttrinvd_det.xxcsttrinvd_site = site
						   and xxcsttrinvd_det.xxcsttrinvd_year = li_year
						   and xxcsttrinvd_det.xxcsttrinvd_period = li_period
						   and xxcsttrinvd_det.xxcsttrinvd_part = t_lddet2_part
						   and xxcsttrinvd_det.xxcsttrinvd_loc = t_lddet2_loc

						   no-error.
	if not available xxcsttrinvd_det then do:
      create xxcsttrinvd_det.
	  assign xxcsttrinvd_det.xxcsttrinvd_domain = global_domain
	         xxcsttrinvd_det.xxcsttrinvd_entity = entity
	         xxcsttrinvd_det.xxcsttrinvd_site = site
	         xxcsttrinvd_det.xxcsttrinvd_year = li_year
	         xxcsttrinvd_det.xxcsttrinvd_period = li_period
			 xxcsttrinvd_det.xxcsttrinvd_part = t_lddet2.t_lddet2_part
	         xxcsttrinvd_det.xxcsttrinvd_loc = t_lddet2.t_lddet2_loc.
	end.
	 assign xxcsttrinvd_det.xxcsttrinvd_end_inv  = t_lddet2.t_lddet2_qty
	        xxcsttrinvd_det.xxcsttrvar_mtl_std_end = t_lddet2.t_lddet2_mtl
            xxcsttrinvd_det.xxcsttrvar_lbr_std_end = t_lddet2.t_lddet2_lbr
            xxcsttrinvd_det.xxcsttrvar_bdn_std_end = t_lddet2.t_lddet2_bdn
            xxcsttrinvd_det.xxcsttrvar_ovh_std_end = t_lddet2.t_lddet2_ovh
            xxcsttrinvd_det.xxcsttrvar_sub_std_end = t_lddet2.t_lddet2_sub.

	  
	end.


	{us/mf/mfselprt.i "printer" 132}	
	for each xxcsttrinvd_det.
	export xxcsttrinvd_det.
	end.
	{us/mf/mfrtrail.i}
end.

PROCEDURE Get_Inv_Qty:

define input parameter site       like ld_site      no-undo.
/*
define input parameter part       like pt_part      no-undo.
*/
define input parameter as_of_date like tr_hist.tr_effdate no-undo.
DEFINE PARAMETER BUFFER buf_t_lddet2 FOR t_lddet2.

define variable loc        like ld_loc       no-undo.
define variable loc1       like ld_loc       no-undo.
 
define variable neg_qty       like mfc_logical initial yes
   label "Include Negative Inventory" no-undo.
define variable net_qty       like mfc_logical initial yes
   label "Include Non-nettable Inventory" no-undo.
define variable inc_zero_qty  like mfc_logical initial no
   label "Include Zero Quantity" no-undo.
define variable zero_cost     like mfc_logical initial yes
   label "Accept Zero Initial Cost" no-undo.
define variable l_recalculate like mfc_logical initial yes
   label "Recalculate Deleted Locations" no-undo.
 
define variable total_qty_oh      like in_qty_oh  no-undo.
 
define variable l_nettable   like mfc_logical no-undo.
define variable l_avail_stat like mfc_logical no-undo.
define variable l_non_consign_qoh like in_qty_oh no-undo.
define variable l_temp_qty_loc    like tr_hist.tr_qty_loc no-undo.
 
define buffer trhist for tr_hist.
define buffer b_trhist for tr_hist.
define variable l_cust_consignqty  like in_qty_oh no-undo.
define variable l_supp_consignqty  like in_qty_oh no-undo.
define variable l_trnbr            like tr_hist.tr_trnbr no-undo.
define variable l_trnbr1            like tr_hist.tr_trnbr no-undo.
 
/* CONSIGNMENT VARIABLES */
{us/po/pocnvars.i}
{us/po/pocnvar2.i}


/* REPORT CURRENCY HEADER DEFINITION 
{us/pp/ppptscfr.i} */
 
/* DETERMINE IF CUSTOMER CONSIGNMENT IS ACTIVE */
{us/bbi/gprun.i ""gpmfc01.p""
   "(input ENABLE_CUSTOMER_CONSIGNMENT,
     input 10,
     input ADG,
     input CUST_CONSIGN_CTRL_TABLE,
     output using_cust_consignment)"}
 
   /* DETERMINE IF SUPPLIER CONSIGNMENT IS ACTIVE */
   {us/bbi/gprun.i ""gpmfc01.p""
   "(input ENABLE_SUPPLIER_CONSIGNMENT,
     input 11,
     input ADG,
     input SUPPLIER_CONSIGN_CTRL_TABLE,
     output using_supplier_consignment)"}
  
   /* INITIALIZING TEMP-TABLES T_TRHIST T_LDDET AND T_STAT */
   empty temp-table t_trhist.
   empty temp-table t_lddet.
   empty temp-table t_stat.
   empty temp-table buf_t_lddet2.
   empty temp-table t_sct.
   
   if loc1 = "" then loc1 = hi_char.
   
   for each in_mstr
      fields(in_domain in_part in_site in_gl_set in_gl_cost_site)
      use-index in_site
   no-lock
      where in_mstr.in_domain = global_domain
	  /*
      and  in_part = part 
      */
	  and   in_site = site,
      first pt_mstr
        
      no-lock
         where pt_mstr.pt_domain = global_domain
         and  pt_part          = in_part,
      each si_mstr where si_domain = global_domain
                   and   si_site   = in_site
      no-lock
   break by in_site by in_part:
 
      if debug-prog then 
	  status default in_part + " ADD".
	  /*** as_of_date cost start by gaoyan ***/
	  l_trnbr1 = 0.
      for first t_sct
         where t_sct_part = in_part no-lock:
      end. /* FOR FIRST T_SCT */
 
     if not available t_sct then do:	  
	
        for first tr_hist
           where tr_hist.tr_domain  =  global_domain
           and   tr_hist.tr_part    =  in_part
           and   tr_hist.tr_effdate >= as_of_date + 1
           and   tr_hist.tr_site    =  in_site
           and   tr_hist.tr_type    =  "CST-ADJ"
        no-lock use-index tr_part_eff:
        end. /* FOR FIRST tr_hist */
       
     
     if available tr_hist then do:
        cst_date = tr_effdate.
        /* GET THE FIRST RECORD ENTERED EVEN IF tr_hist.tr_PART_EFF*/
        /* ISN'T IN TRANSACTION NUMBER SEQUENCE            */
 
	   for each tr_hist  where tr_hist.tr_domain = global_domain
              and   tr_hist.tr_part    = in_part
              and   tr_hist.tr_effdate = cst_date
			  and   tr_hist.tr_type  =  "CST-ADJ"
              and   tr_hist.tr_site    = in_site
			  no-lock use-index tr_part_eff
         by tr_trnbr:
         trrecno = recid(tr_hist).
		 l_trnbr1 = tr_hist.tr_trnbr.
         leave.
		 
	   end.
       
	   for last tr_hist where tr_hist.tr_domain = global_domain
	                      and tr_hist.tr_part = in_part
						  and tr_hist.tr_trnbr < l_trnbr1
						  and tr_hist.tr_site = in_site
						  no-lock.
	   end.
       if available tr_hist then do:

              create t_sct.
              assign
                 t_sct_part      = in_part
                 t_sct_desc1     = pt_desc1
                 t_sct_desc2     = pt_desc2
                 t_sct_abc       = pt_abc
                 t_sct_um        = pt_um
     			 t_sct_mtl       = tr_hist.tr_mtl_std 
                 t_sct_lbr       = tr_hist.tr_lbr_std
                 t_sct_bdn       = tr_hist.tr_bdn_std
                 t_sct_ovh       = tr_hist.tr_ovh_std
                 t_sct_sub       = tr_hist.tr_sub_std
				 t_sct_tot       = tr_hist.tr_mtl_std 
				                   + tr_hist.tr_lbr_std
				                   + tr_hist.tr_bdn_std
				                   + tr_hist.tr_ovh_std
				                   + tr_hist.tr_sub_std.				 				 				 
        end. 
	
	
     
     end. /* IF AVAILABLE tr_hist */
   else do:
   /** get cost from sct_det **/
    if not available icc_ctrl then
    for first icc_ctrl  where icc_ctrl.icc_domain = global_domain no-lock:
    end.
	
    /* GET GL COSTS BY in_gl_cost_site*/
   if available in_mstr then do:
     
   if in_gl_set = "" then do:
         for first sct_det 
            where sct_det.sct_domain = global_domain and  sct_sim = icc_gl_set
             and sct_part = in_part
             and sct_site = in_gl_cost_site no-lock: end.
    end.
    else do:
         for first sct_det 
            where sct_det.sct_domain = global_domain and  sct_sim = in_gl_set
            and sct_part = in_part
             and sct_site = in_gl_cost_site no-lock: end.
    end.
    if available sct_det  
    then do:
              create t_sct.
              assign
                 t_sct_part      = in_part
                 t_sct_desc1     = pt_desc1
                 t_sct_desc2     = pt_desc2
                 t_sct_abc       = pt_abc
                 t_sct_um        = pt_um
     			 t_sct_mtl       = sct_mtl_ll + sct_mtl_tl
                 t_sct_lbr       = sct_lbr_ll + sct_lbr_tl
                 t_sct_bdn       = sct_bdn_ll + sct_bdn_tl
                 t_sct_ovh       = sct_ovh_ll + sct_ovh_tl
                 t_sct_sub       = sct_sub_ll + sct_sub_tl	
				 t_sct_tot       = t_sct_mtl
				                   + t_sct_lbr
				                   + t_sct_bdn
				                   + t_sct_ovh
				                   + t_sct_sub.				 
	 end. /* if available sct_det */
   end. /* if available in_mstr */
  end. /* else do if there is no cost adjust trasaction after effdate */   
end. /* IF NOT AVAILABLE T_SCT */


	  /*** as_of_date cost end ***/
 
      l_trnbr = 0.
      
      for last tr_hist
         where tr_hist.tr_domain =  global_domain
         and   tr_hist.tr_trnbr  <> 0
      no-lock:
      end. /* FOR LAST tr_hist */
      if available tr_hist 
         and tr_hist.tr_trnbr  <> 0
      then
         l_trnbr = tr_hist.tr_trnbr.
 
       for each ld_det
            no-lock
             where ld_det.ld_domain = global_domain and  ld_part = pt_part
            and   ld_site = in_site
            and   (ld_loc >= loc and ld_loc <= loc1):
 
       find first t_lddet exclusive-lock
            where t_lddet_part = ld_part
            and   t_lddet_site = ld_site
            and   t_lddet_loc  = ld_loc no-error.
 
         if not available t_lddet then do:
            create t_lddet.
            assign
               t_lddet_site             = ld_site
               t_lddet_cust_consign_qty = ld_cust_consign_qty
               t_lddet_supp_consign_qty = ld_supp_consign_qty
               t_lddet_part             = ld_part
               t_lddet_loc              = ld_loc.
         end.
         else
            assign
               t_lddet_cust_consign_qty = t_lddet_cust_consign_qty
                                        + ld_cust_consign_qty
               t_lddet_supp_consign_qty = t_lddet_supp_consign_qty
                                        + ld_supp_consign_qty.
 
         run ck_status(input  ld_status,
            output l_avail_stat,
            output l_nettable).
 
         if net_qty = yes or not l_avail_stat
            or (l_avail_stat and l_nettable) then
            t_lddet_qty = t_lddet_qty + ld_qty_oh.
 
      end. /* FOR EACH LD_DET */
 
      /*CREATING t_trhist RECORDS.*/
      for each tr_hist
         fields(tr_hist.tr_domain tr_hist.tr_part tr_hist.tr_effdate tr_hist.tr_site tr_hist.tr_loc tr_hist.tr_trnbr
         tr_hist.tr_ship_type tr_hist.tr_nbr tr_hist.tr_type tr_hist.tr_program tr_hist.tr_qty_loc tr_hist.tr_status
         tr_hist.tr_rmks tr_hist.tr_rev )
         where  tr_hist.tr_domain     = global_domain
            and tr_hist.tr_part       = pt_part
            and tr_hist.tr_effdate    > as_of_date
            and tr_hist.tr_site       = in_site
            and (tr_hist.tr_loc      >= loc
            and  tr_hist.tr_loc      <= loc1)
            and tr_hist.tr_trnbr     <= l_trnbr
            and tr_hist.tr_ship_type  = ""
      no-lock:
         create t_trhist.
         assign
            t_trhist_domain             = tr_hist.tr_domain
            t_trhist_part               = tr_hist.tr_part
            t_trhist_effdate            = tr_hist.tr_effdate
            t_trhist_site               = tr_hist.tr_site
            t_trhist_loc                = tr_hist.tr_loc
            t_trhist_trnbr              = tr_hist.tr_trnbr
            t_trhist_ship_type          = tr_hist.tr_ship_type
            t_trhist_nbr                = tr_hist.tr_nbr
            t_trhist_type               = tr_hist.tr_type
            t_trhist_program            = tr_hist.tr_program
            t_trhist_qty_loc            = tr_hist.tr_qty_loc
            t_trhist_status             = tr_hist.tr_status
            t_trhist_rmks               = tr_hist.tr_rmks
            t_trhist_rev                = tr_hist.tr_rev
            . 
      end. /* FOR EACH tr_hist */
      /* ADDED tr_hist LOOP TO CREATE TEMP-TABLE t_lddet FOR ITEMS  */
      /* HAVING ZERO QOH FOR WHICH ld_det DOES NOT EXIST. */
 
      if l_recalculate
      then do:
         for each tr_hist
            fields(tr_hist.tr_domain tr_hist.tr_part tr_hist.tr_site tr_hist.tr_loc tr_hist.tr_status tr_hist.tr_ship_type
                   tr_hist.tr_lot tr_hist.tr_ref tr_hist.tr_qty_loc tr_hist.tr_effdate )
         no-lock
            where tr_hist.tr_domain     = global_domain
            and   tr_hist.tr_part       = pt_part
            and   tr_hist.tr_site       = in_site
            and   tr_hist.tr_effdate   >= as_of_date
            and   tr_hist.tr_ship_type  = ""
            and   tr_hist.tr_loc       >= loc
            and   tr_hist.tr_loc       <= loc1
            and   not can-find(first ld_det
                         where ld_domain = global_domain
                         and   ld_part   = pt_part
                         and   ld_site   = in_site
                         and   ld_loc    = tr_hist.tr_loc)
         break by tr_hist.tr_part by tr_hist.tr_site by tr_hist.tr_loc :
 
            if first-of(tr_hist.tr_loc)
            then do:
 
               find first t_lddet
                  where t_lddet_part = tr_hist.tr_part
                  and   t_lddet_site = tr_hist.tr_site
                  and   t_lddet_loc  = tr_hist.tr_loc
               exclusive-lock no-error.
 
               if not available t_lddet
               then do:
                  create t_lddet.
                  assign
                     t_lddet_site = tr_hist.tr_site
                     t_lddet_part = tr_hist.tr_part
                     t_lddet_loc  = tr_hist.tr_loc
                     t_lddet_qty  = 0.
               end. /* IF NOT AVAILABLE t_lddet */
 
            end. /* IF first-of(tr_hist.tr_loc) */
 
         end.  /* FOR EACH tr_hist.. */
 
      end. /* IF l_recalculate */
 
      if last-of(in_site) then do:
 
         for each t_lddet exclusive-lock
               where t_lddet_site = in_site
               break by t_lddet_loc by t_lddet_part:
 
            if first-of(t_lddet_part) then
            assign
               tot_cust_consign_qty = 0
               tot_supp_consign_qty = 0
               total_qty_oh         = 0
               l_non_consign_qoh    = 0
               l_supp_consignqty    = 0
               l_cust_consignqty    = 0
               
               .
 
             l_non_consign_qoh = l_non_consign_qoh
                                 + t_lddet_qty
                                 - t_lddet_cust_consign_qty
                                 - t_lddet_supp_consign_qty.
 
            /* THE VARIABLES total_qty_oh,tot_cust_consign_qty AND    */
            /* tot_supp_consign_qty ARE CALCULATED TO STORE THE       */
            /* QUANTITY ON HAND, CUSTOMER CONSIGNED QTY               */
            /* AND SUPPLIER CONSIGNED QTY RESPECTIVELY.               */
 
            if net_qty = yes or not t_lddet_avail_stat
               or (t_lddet_avail_stat and t_lddet_net)
            then do:
 
               total_qty_oh = total_qty_oh + t_lddet_qty.
 
               /*DETERMINE CONSIGNMENT QUANTITIES */
               if using_cust_consignment
               then
                  tot_cust_consign_qty = tot_cust_consign_qty
                                       + t_lddet_cust_consign_qty.
 
               if using_supplier_consignment
               then
                  tot_supp_consign_qty = tot_supp_consign_qty
                                         + t_lddet_supp_consign_qty.
 
            end. /* If net_qty = yes or not t_lddet_avail_status */
 
            /* PRINTS SUMMARIZED QTY FOR AN ITEM IN A LOCATION */
            if last-of(t_lddet_part)
            then do:
               /* BACK OUT tr_hist AFTER AS-OF DATE */
               for each t_trhist
                  where t_trhist_domain    = global_domain
                  and   t_trhist_part      = t_lddet_part
                  and   t_trhist_effdate   > as_of_date
                  and   t_trhist_site      = t_lddet_site
                  and   t_trhist_loc       = t_lddet_loc
                  and   t_trhist_trnbr    <= l_trnbr
                  and   t_trhist_ship_type = ""
               no-lock:
 
                  if t_trhist_qty_loc = 0
                     and t_trhist_type <> "CN-SHIP"
                     and t_trhist_type <> "CN-USE"
                     and t_trhist_type <> "CN-ADJ"
                  then
                     next.
 
                  run ck_status(input  t_trhist_status,
                                output l_avail_stat,
                                output l_nettable).
 
                  if (net_qty = yes
                     or not l_avail_stat
                     or (l_avail_stat
                     and l_nettable))
                  then
                     if t_trhist_type <> "CN-ADJ"
                     then
                        total_qty_oh = total_qty_oh - t_trhist_qty_loc.
 
                  /* NOTE: CN-ADJ DOES NOT UPDATE THE tr_hist.tr_qty_loc AND NO */
                  /* OTHER TRANSACTIONS ARE CREATED LIKE THOSE CREATED  */
                  /* AT THE TIME OF CN-SHIP OR CN-USE                   */
 
                  /*UAT-CR3*/
                  if (using_cust_consignment or using_supplier_consignment) then do:
                  
                     if     t_trhist_type    = "CN-ADJ"
                        and t_trhist_program = "socnadj.p"
                     then
                        tot_cust_consign_qty = tot_cust_consign_qty
                                             - t_trhist_qty_cn_adj.
                     else
                     if t_trhist_type = "CN-SHIP"
                        or t_trhist_type = "CN-USE"
                     then do:
                        for first trhist
                           fields(trhist.tr_domain trhist.tr_trnbr trhist.tr_qty_loc)
                           where trhist.tr_domain = global_domain
                           and   trhist.tr_trnbr  = int64(t_trhist.t_trhist_rmks)
                        no-lock:
                           l_temp_qty_loc = trhist.tr_qty_loc.
                        end. /* FOR FIRST trhist */
    
                        for first trhist
                           fields(trhist.tr_domain trhist.tr_trnbr trhist.tr_type trhist.tr_rmks)
                           where trhist.tr_domain      = global_domain
                           and   trhist.tr_trnbr       > int64(t_trhist.t_trhist_rmks)
                           and  (trhist.tr_type        = "CN-USE" or
                                 trhist.tr_type        = "CN-SHIP")
                           and integer(trhist.tr_rmks) = integer(t_trhist.t_trhist_rmks)
                        no-lock:
                           if trhist.tr_trnbr = t_trhist.t_trhist_trnbr
                           then do:
                              assign
                                 tot_cust_consign_qty = tot_cust_consign_qty
                                                        - l_temp_qty_loc
                                 l_non_consign_qoh    = l_non_consign_qoh
                                                        + l_temp_qty_loc.
                           end. /* IF trhist.tr_trnbr.. */
                        end. /* FOR FIRST trhist */
                     end. /* IF t_trhist_type = CN-SHIP */
                     else
                     if t_trhist_type = "ISS-TR"
                     then do:
                        /* FIRST ADD TRANSFER QTY TO NON-CONSIGNED INVENTORY. */
                        /* t_trhist_qty_loc IS NEGATIVE FOR ISS-TR TRANSACTIONS.    */
                        l_non_consign_qoh = l_non_consign_qoh - t_trhist_qty_loc.
    
                        /* NEXT, FIND OUT IF THE TRANSFER IS A SUPPLIER */
                        /* CONSIGNMENT TRANSFER WITHOUT CHANGE OF       */
                        /* OWNERSHIP. IF SO, THEN UPDATE SUPPLIER       */
                        /* CONSIGNMENT AND NON-CONSIGNMENT INVENTORY.   */
                        /* IN THE CASE OF A SUPPLIER CONSIGNED TRANSFER */
                        /* WITHOUT OWNERSHIP CHANGE, THE TRANSACTION    */
                        /* HISTORY NUMBER AND THE TRANSFER QTY WILL BE  */
                        /* STORED IN THE t_trhist_rev FIELD OF THE RCT-TR.    */
                        for first trhist
                            fields(trhist.tr_trnbr trhist.tr_qty_loc trhist.tr_rev trhist.tr_type trhist.tr_effdate)
                            where trhist.tr_domain = global_domain
                              and trhist.tr_trnbr > t_trhist.t_trhist_trnbr
                              and trhist.tr_type = "RCT-TR"
                              and trhist.tr_effdate = t_trhist.t_trhist_effdate
                                             and
                       int64(substring(trhist.tr_rev,1,index(trhist.tr_rev," ") - 1)) =
                       t_trhist.t_trhist_trnbr
                       no-lock:
    
                       assign
                          tot_supp_consign_qty = tot_supp_consign_qty +
                          decimal(trim(substring(trhist.tr_rev,index(trhist.tr_rev," ") + 1,length(trhist.tr_rev))))
                          l_non_consign_qoh = l_non_consign_qoh -
                          decimal(trim(substring(trhist.tr_rev,index(trhist.tr_rev," ") + 1,length(trhist.tr_rev)))).
                        end. /* FOR FIRST trhist */
                     end. /* IF t_trhist_type = "ISS-TR" */
                     else
                     if t_trhist_type = "RCT-TR"
                     then do:
                        /* FIRST BACK OUT TRANSFER QTY FROM NON-CONSIGNED */
                        /* INVENTORY.                                     */
                        l_non_consign_qoh = l_non_consign_qoh - t_trhist_qty_loc.
    
                        /* NEXT, FIND OUT IF THE TRANSFER IS A SUPPLIER */
                        /* CONSIGNMENT TRANSFER WITHOUT CHANGE OF       */
                        /* OWNERSHIP. IF SO, THEN UPDATE SUPPLIER       */
                        /* CONSIGNMENT AND NON-CONSIGNMENT INVENTORY.   */
                        /* IN THE CASE OF A SUPPLIER CONSIGNED TRANSFER */
                        /* WITHOUT OWNERSHIP CHANGE, THE TRANSACTION    */
                        /* HISTORY NUMBER AND THE TRANSFER QTY WILL BE  */
                        /* STORED IN THE t_trhist_rev FIELD OF THE RCT-TR.    */
                        if t_trhist_rev <> "" then do:
                           assign
                              tot_supp_consign_qty = tot_supp_consign_qty -
                              decimal(trim(substring(t_trhist_rev,index(t_trhist_rev," ") + 1,length(t_trhist_rev))))
                              l_non_consign_qoh = l_non_consign_qoh +
                              decimal(trim(substring(t_trhist_rev,index(t_trhist_rev," ") + 1,length(t_trhist_rev)))).
                        end.
                     end. /* IF t_trhist_type = "RCT-TR" */
                     else
                     if    t_trhist_type = "CN-RCT"
                        or t_trhist_type = "CN-ISS"
                     then
                        tot_supp_consign_qty  = tot_supp_consign_qty
                                              - t_trhist_qty_loc.
                     else
                     if     t_trhist_type    = "CN-ADJ"
                        and t_trhist_program = "pocnadj.p"
                     then
                        tot_supp_consign_qty  = tot_supp_consign_qty
                                              - t_trhist_qty_cn_adj.
                     else
                         l_non_consign_qoh = l_non_consign_qoh  - t_trhist_qty_loc.
 
                  end. /* (using_cust_consignment or using_supplier_consignment) */
               end. /* FOR EACH t_trhist */
 
               /* SINCE THE VALUES IN THE VARIABLES total_qty_oh,        */
               /* tot_cust_consign_qty and tot_supp_consign_qty HAVE     */
               /* QTY ON HAND, CUSTOMER CONSIGNED AND SUPPLIER CONSIGNED */
               /* QTY RESPECTIVELY,THE CONSIGNED QTY VARIABLES ARE ADDED */
               /* SUBTRACTED IN CASE OF 'EXCLUDE' FROM total_qty_oh OR   */
               /* ASSIGNED TO total_qty_oh IN CASE OF 'ONLY' AND IN CASE */
               /* INCLUDE THE VARIABLES ARE DISPLAYED ITSELF BECAUSE     */
               /* THEY HAVE THE RESPECTIVE VALUES.                       */
 
               if using_cust_consignment
               then do:
                  if customer_consign_code = EXCLUDE
                  then
                     total_qty_oh = total_qty_oh - tot_cust_consign_qty.
 
               end. /* IF using_cust_consignment... */
 
               if using_supplier_consignment
               then do:
 
                  if supplier_consign_code = EXCLUDE
                  then do:
                     if (not using_cust_consignment)
                        or (using_cust_consignment
                           and customer_consign_code <> ONLY)
                     then
                        total_qty_oh = total_qty_oh - tot_supp_consign_qty.
                  end. /* IF supplier_consign_code = EXCLUDE */
 
               end. /* IF using_supplier_consignment... */
 
               /* CALCULATE THE EXTENDED COST BASED ON TOTAL QTY ON-HAND */
               
               /* THE CONDITION FOR CHECKING total_qty_oh HAS BEEN MOVED */
               /* FROM ABOVE BECAUSE total_qty_oh WOULD HAVE THE CORRECT */
               /* VALUE ONLY AFTER BACKING OUT THE QTY.                  */
 
               if total_qty_oh     > 0
                  or (total_qty_oh = 0
                      and inc_zero_qty)
                  or (total_qty_oh < 0
                      and neg_qty)
               then do:
 
                  /* display
                     t_lddet_part
                     total_qty_oh 
                     t_lddet_qty
                     t_lddet_loc
                     .*/
                     
                  t_lddet_qty = total_qty_oh.
                  FIND t_sct where t_sct_part = t_lddet_part no-error.
                  FIND FIRST buf_t_lddet2 WHERE buf_t_lddet2.t_lddet2_site = t_lddet_site
                                        AND buf_t_lddet2.t_lddet2_part = t_lddet_part
                                        AND buf_t_lddet2.t_lddet2_loc  = t_lddet_loc 
                                        NO-ERROR.
                  IF NOT AVAILABLE buf_t_lddet2 THEN
                  DO:
                     CREATE buf_t_lddet2.
                     ASSIGN buf_t_lddet2.t_lddet2_site = t_lddet_site
                            buf_t_lddet2.t_lddet2_part = t_lddet_part
                            buf_t_lddet2.t_lddet2_loc  = t_lddet_loc 
                            buf_t_lddet2.t_lddet2_qty  = total_qty_oh
							buf_t_lddet2.t_lddet2_tot = t_sct_tot when available t_sct
                            buf_t_lddet2.t_lddet2_mtl = t_sct_mtl when available t_sct
                            buf_t_lddet2.t_lddet2_lbr = t_sct_lbr when available t_sct
                            buf_t_lddet2.t_lddet2_bdn = t_sct_bdn when available t_sct
                            buf_t_lddet2.t_lddet2_ovh = t_sct_ovh when available t_sct
                            buf_t_lddet2.t_lddet2_sub = t_sct_sub when available t_sct.
                  END. /* IF NOT AVAILABLE t_lddet2 */
                  
                  if customer_consign_code = EXCLUDE
                  then
                     l_cust_consignqty = 0.
                  
 
                  if supplier_consign_code = EXCLUDE
                  then
                     l_supp_consignqty = 0.
                   
                  l_non_consign_qoh = (total_qty_oh - l_cust_consignqty -
                                       l_supp_consignqty).
 
               end. /* IF total_qty_oh > 0 OR ... */
            end. /* IF LAST-OF(t_lddet_part) */
 
            delete t_lddet.
 
         end. /* FOR EACH T_LDDET */
 
      end. /* IF LAST-OF(IN_SITE) */
 
   end. /* FOR EACH IN_MSTR */
   
END PROCEDURE. /* Get_Inv_Qty */

 /* PROCEDURE TO FIND THE INVENTORY STATUS IN TEMP TABLE T_STAT   */
/* IF NOT AVAILABLE THEN SEARCH IN IS_MSTR, THIS AVOIDS MULTIPLE */
/* SCANNING OF IS_MSTR                                           */
 
PROCEDURE ck_status:
 
   define input  parameter pr_status     like is_status   no-undo.
   define output parameter pr_avail_stat like mfc_logical no-undo.
   define output parameter pr_nettable   like mfc_logical no-undo.
 
   for first t_stat
      where t_stat_stat = pr_status
   no-lock:
   end. /* FOR FIRST T_STAT */
 
   if not available t_stat then do:
 
      for first is_mstr
         fields( is_domain is_status is_nettable)
          where is_mstr.is_domain = global_domain and  is_status = pr_status
          no-lock:
      end. /* FOR FIRST IS_MSTR */
 
      if available is_mstr then do:
 
         create t_stat.
         assign
            t_stat_stat   = is_status
            t_stat_net    = is_nettable
            pr_nettable   = is_nettable
            pr_avail_stat = yes.
      end. /* IF AVAILABLE IS_MSTR */
      else
      assign
         pr_nettable   = no
         pr_avail_stat = no.
   end. /* IF NOT AVAILABLE T_STAT */
   else
   assign
      pr_nettable   = t_stat_net
      pr_avail_stat = yes.
 
END PROCEDURE. /* PROCEDURE CK_STATUS */